package com.ftwj.demo.permission.controller;

import java.util.Collection;
import java.util.Date;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ftwj.demo.global.shiro.RetryLimitHashedCredentialsMatcher;
import com.ftwj.demo.global.shiro.ShiroPrincipal;
import com.ftwj.demo.utils.result.ResultBean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
*
* <p>Title: ShiroController</p>  
* <p>Description: 描述:：Shiro操作控制</p>
* <p>Copyright: Copyright (c) 2020 </p>  
* <p>Company: 兰州飞天网景信息产业有限公司</p>    
* @author WangSLi
* @date 2020年5月29日 下午5:37:49
* @version V1.0
 */
@RestController
@RequestMapping("/shiro")
@Api(tags="SHIRO管理接口", value="SHIRO管理")
@Validated
public class ShiroController {
	
	@Autowired
	private RetryLimitHashedCredentialsMatcher retryLimitHashedCredentialsMatcher;
	@Autowired
	private SessionDAO sessionDAO;
	
	/**
	 * 	解除用户的限制登录 
	 * @return
	 */
	@PostMapping("/unlock")
	@ApiOperation(value="解锁已锁定的用户（DONE）", notes="备注",code=0,httpMethod="GET")
	public ResultBean<?> unlockAccount(@RequestParam String phone){
	    retryLimitHashedCredentialsMatcher.unlockAccount(phone);
	    return ResultBean.success();
	}
	
	@GetMapping("/user")
	public ResultBean<?> onlineUser(){
		Collection<Session> sessions = sessionDAO.getActiveSessions();
		for (Session session : sessions) {
			String host = session.getHost();
			Date time = session.getLastAccessTime();
			SimplePrincipalCollection principal = (SimplePrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
			Object r = principal.getPrimaryPrincipal();
		}
		return ResultBean.success();
	}
}
